import type { SogouElements } from '../../../../../../../../types/constants/personalization/sogou'
import type { ThemeType } from '../../../../../../../../types/interfaces'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import {
  AppStyleCollections,
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { SogouCommonPage } from './common'

export class SogouWebPage {
  elements: SogouElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.SO_GOU]
  private static instance: SogouWebPage
  protected readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  private commonPage: SogouCommonPage = SogouCommonPage.getInstance()

  /**
   * 获取单例实例
   */
  public static getInstance(): SogouWebPage {
    if (!SogouWebPage.instance) {
      SogouWebPage.instance = new SogouWebPage()
    }
    return SogouWebPage.instance
  }

  init(): void {
  }

  encyclopediaStyles(): string {
    return `
/* 搜狗百科 */
#sogou_wrap_id #wrapper #main .results {
  > .${AppStyleCollections.Sogou.SOGOU_ENCYCLOPEDIA} {
    .title-newblue {
      background-image: none !important;
      border: none !important;
      
      h3 {
        span {
          color: var(${this.themeVarService.em.text}) !important;
        }
        
        p {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
      }
      
      .border-top {
        border-top: none !important;
      }
    }
  }
  
  /* 精选内容*/
  .vrwrap {
    .special-wrap {
      background-image: none !important;
      border: none !important;
      padding: 0 !important;
      
      .ai-search-bar-pc-title {
        color: var(${this.themeVarService.search.default.text}) !important;
        
        .ai-search-bar-pc-logo-text {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
      }
      
      .special-title {
        h3 {
          span {
            color: var(${this.themeVarService.em.text}) !important;
          }
        }
      }
    }
  }
}
`
  }

  askStyles(): string {
    return `
/* 搜狗问问 */
#sogou_wrap_id #wrapper #main .results {
  > .${AppStyleCollections.Sogou.SOGOU_ASK} {
    .tab-box {
      border-bottom: none !important;
      
      a {
        border: none !important;
        color: var(${this.themeVarService.search.default.text}) !important;
        
        &.cur {
          color: var(${this.themeVarService.message.success.textPrimary}) !important;
        }
      }
    }
    
    .tab-box:has(a:not(.cur)) {
      a {
        &.cur {
          border-bottom: 1px solid var(${this.themeVarService.message.info.textPrimary}) !important;
        }
      }
    }
    
    .tab-main { 
      .best-answer {
        p {
          /* 展开/收起回答 */
          a[id*=_down], a[id*=_up]  {
            background-color: transparent !important;
            color: var(${this.themeVarService.link.default.text}) !important;
          }
        }
      }
    }
  }
}
`
  }

  videoStyles(): string {
    return `
/* 精选视频 */
#sogou_wrap_id #wrapper #main .results {
  > .${AppStyleCollections.Sogou.FEATURED_VIDEOS} {
    .vrwrap {
      border: none !important;
      width: 100% !important;
      
      /* 结果项标题 */
      a[class*=titleWrap] {
        color: var(${this.themeVarService.link.default.text}) !important;
        
        &:hover {
          color: var(${this.themeVarService.link.default.textHover}) !important;
          text-decoration: none !important;
          
          em {
            text-decoration: none !important;
          }
        }
      }
      
      /* 视频标题 */
      a[class*=video-list__sourceLink] {
        color: var(${this.themeVarService.link.default.text}) !important;
        
        &:hover {
          color: var(${this.themeVarService.link.default.textHover}) !important;
          text-decoration: none !important;
          
          em {
            text-decoration: none !important;
          }
        }
      }
      
      /* 底部链接 */
      > a[class*=footer] {
        color: var(${this.themeVarService.link.default.text}) !important;
        
        &:hover {
          color: var(${this.themeVarService.link.default.textHover}) !important;
          text-decoration: none !important;
        }
      }
    }
  }
}
`
  }

  /**
   * 无搜索结果样式
   */
  noSearchResult(): string {
    return ``
  }

  getMainStyles(theme: ThemeType): string {
    return `
${this.commonPage.baseWebResultStyles(theme)}
/* 顶部菜单 */
#sogou_wrap_id {
  .header {
    #searchForm {
      .sbox-history-container, .sbox-sugg-container {
        font-size: ${this.themeVarService.search.default.fontSize} !important;
      }
      
      .sbox-history-container {
        color: var(${this.themeVarService.search.history.text}) !important;
      }
      
      .sbox-sugg-container {
        color: var(${this.themeVarService.search.suggestions.text}) !important;
      }
    }
  }
  
  #wrapper {
    #main {
      /* 搜索结果列表 */
      .results {
        /* 电影 */
        .${AppStyleCollections.Sogou.MOVIE} {
          .sa-color-bg-white {
            background: transparent !important;
          }
          .sa-color-text-primary {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
          
          [class^=movie-struct__buttonText] {
            .sa-text-clamp1 {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }
        }

        /* 短剧热播榜 */
        .${AppStyleCollections.Sogou.SHORT_VIDEO_HOT_LIST} {
          .vrwrap {
            border: none !important;
            background: transparent !important;
            width: 100% !important;
            
            /* 结果项标题 */
            span[class*=mainTitle] {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
            
            /* 短剧名称 */
            span[class*=shortPlaysName] {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
          }
        }
        
        /* 搜狗汉语 */
        .${AppStyleCollections.Sogou.SOGOU_CHINESE} {
          .vrwrap {
            > div > div {
              border: none !important;
            }
            
            [class*=header__moudleCardContainer] {
              background: transparent !important;
              
              [class^=header__svgBox] {
                svg {
                  path {
                    fill: #ff4800 !important;
                  }
                }
              }
            }
            
            [data-testid="sa-footer"] {
              border-top: none !important;
            }
            
            div[class*=pcLiju], span {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
            
            h3[class*="title__titleContainer"] {
              span {
                color: var(${this.themeVarService.link.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.textHover}) !important;
                  text-decoration: none !important;
                }
              }
            }
          }
        }
        
        /* 搜狗图片 */
        .${AppStyleCollections.Sogou.SOGOU_IMAGE} {
          .vrwrap {
            > div {
              border: none !important;
            }
            
            h3 {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
          }
        }
        
        /* 搜狗下载 */
        .${AppStyleCollections.Sogou.SOGOU_DOWNLOAD} {
          .tab-base {
            background: transparent !important;
            
            ul {
              li {
                a {
                  color: var(${this.themeVarService.search.default.text}) !important;
                }
                
                i {
                  background: var(${this.themeVarService.tab.textActive}) !important;
                }
                
                &.active {
                  a {
                    color: var(${this.themeVarService.tab.textActive}) !important;
                  }
                }
              }
            }
          }
          
          .table-container {
            border-bottom: none !important;
            
            table {
              border-collapse: separate !important;
              border-spacing: 0 !important;
              
              p {
                a {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }
              
              .img-height {
                height: auto !important;
              }
            }
          }
          
          .more-link {
            a {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }
        }
        
        
        /* 相关资讯 */
        .${AppStyleCollections.Sogou.NEWS} {
          [class*=zixun] {
            border: none !important;
            
            h3 + div {
              color: var(${this.themeVarService.search.default.text}) !important;
              
              a {
                color: var(${this.themeVarService.search.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.text}) !important;
                  text-decoration: none !important;
                  
                  em {
                    text-decoration: none !important;
                  }
                }
              }
            }
            
            /* 来源 */
            .my-doc-info {
              a {
                color: var(${this.themeVarService.link.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.textHover}) !important;
                  text-decoration: none !important;
                }
              }
            }
            
            /* 更多 */
            .more-box {
              border-top: none !important;
              
              a[id*=juhe_more] {
                color: var(${this.themeVarService.link.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.textHover}) !important;
                  text-decoration: none !important;
                }
              }
              
              .part-left {
                display: none !important;
              }
              
              .part-right {
                position: relative !important;
              }
            }
          }
        }
      }
    }
  }
}
${this.videoStyles()}
`
  }
}
